package com.pray;




import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * Main
 * <p>
 *
 * @author 春江花朝秋月夜
 * @since 2023/9/8 10:40
 */
public class Main {
    public static void main(String[] args) throws IOException {
        climbContent();
    }
    public static void climbContent()throws IOException{
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入爬取页数");
        int pages = scanner.nextInt(); // 要爬取的评论页数
        scanner.nextLine();
        System.out.println("请输入爬取ID");
        String videoId = scanner.nextLine();//"BV1vu411N7Xy"; // 视频的ID
        System.out.println("请输入爬取关键词");
        String keywords= scanner.nextLine();
        String pattern=".*"+keywords+".*";
        String outPutDes="D:\\JavaWork\\pary_analysis\\doc\\"+videoId+".txt";
        FileOutputStream outputStream = new FileOutputStream(outPutDes);

        for (int i = 1; i <= pages; i++) {
            String url = "https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=" + i + "&type=1&oid=" + videoId;
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                JSONObject jsonResponse = new JSONObject(response.toString());
                if(jsonResponse.getJSONObject("data")==null){
                    continue;
                }
                JSONArray comments = jsonResponse.getJSONObject("data").getJSONArray("replies");
                for (int j = 0; j < comments.size(); j++) {
                    JSONObject comment = comments.getJSONObject(j);
                    String content = comment.getStr("content");
                    JSONObject jsonObject=new JSONObject(content);
                    Matcher matcher= Pattern.compile(pattern).matcher(content);
                    if (matcher.matches()){
                        System.out.println("关键字匹配成功，爬取数据中....");
                        outputStream.write(jsonObject.getStr("message").getBytes());
                    }
                }
            } else {
                System.out.println("请求失败，响应码：" + responseCode);
            }
            connection.disconnect();
        }
        System.out.println("爬取完成!!!");
    }
}
